home *** CD-ROM | disk | FTP | other *** search
- package symantec.itools.db.awt;
-
- import java.awt.Event;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.Rectangle;
- import java.awt.image.ImageObserver;
-
- public class BasicCell implements TableCell, ImageObserver {
- Grid view;
- DataSource dataSource;
- Coordinate coords;
- boolean selected;
- boolean keyPressedYet;
- boolean loseFocusOnArrow;
- int cursorPos;
- int hlFirst;
- int hlLast;
- boolean selectionMade;
- int offset;
- int chopIndex;
- int toLeftOfCell;
- int startX;
- boolean defaultCell;
- int type;
- static final int PADSIDES = 5;
-
- public BasicCell(Grid tv, DataSource ds) {
- this.view = tv;
- this.dataSource = ds;
- }
-
- public TableCell cloneCell() {
- BasicCell bs = new BasicCell(this.view, this.dataSource);
- if (this.coords != null) {
- bs.coords = new Coordinate(this.coords.row, this.coords.col);
- }
-
- bs.selected = this.selected;
- bs.keyPressedYet = this.keyPressedYet;
- bs.loseFocusOnArrow = this.loseFocusOnArrow;
- bs.cursorPos = this.cursorPos;
- bs.selectionMade = this.selectionMade;
- bs.offset = this.offset;
- bs.type = this.type;
- return bs;
- }
-
- public int type() {
- return this.type;
- }
-
- public int type(int t) {
- if (t <= 3 && t >= 0) {
- return this.type = t;
- } else {
- throw new IllegalArgumentException("Invalid cell type");
- }
- }
-
- public void setGrid(Grid v, DataSource ds) {
- this.view = v;
- this.dataSource = ds;
- }
-
- public void setDefaultFlag() {
- this.defaultCell = true;
- }
-
- public void reset() {
- this.selected = false;
- this.keyPressedYet = false;
- this.loseFocusOnArrow = false;
- this.cursorPos = 0;
- this.selectionMade = false;
- this.offset = 0;
- }
-
- public boolean isCellTypeEditable() {
- return true;
- }
-
- public void setCoordinates(Coordinate c) {
- this.coords = c;
- }
-
- public Coordinate getCoordinates() {
- return this.coords;
- }
-
- public int row() {
- return this.coords.row;
- }
-
- public void setRow(int r) {
- this.coords.row = r;
- }
-
- public int col() {
- return this.coords.col;
- }
-
- public void setCol(int c) {
- this.coords.col = c;
- }
-
- public boolean mouseEvent(Event e) {
- try {
- switch (e.id) {
- case 63:
- this.keyPressedYet = false;
- this.view.redraw(true);
- this.offset = 0;
- this.view.generateEvent(e, 63, this);
- break;
- case 501:
- if (!e.shiftDown()) {
- this.keyPressedYet = true;
- this.view.setCapture();
- this.cursorPos = this.findCursorPos(e.x);
- this.selectionMade = false;
- } else {
- this.hlFirst = this.cursorPos;
- this.hlLast = this.findCursorPos(e.x);
- this.selectionMade = true;
- }
-
- this.view.redrawCell(this);
- this.view.generateEvent(e, 61, this);
- break;
- case 502:
- this.view.generateEvent(e, 60, this);
- break;
- case 506:
- if (!this.selectionMade && this.findCursorPos(e.x) != this.cursorPos) {
- this.hlFirst = this.cursorPos;
- this.selectionMade = true;
- }
-
- this.hlLast = this.cursorPos = this.findCursorPos(e.x);
- this.view.redrawCell(this);
- this.view.generateEvent(e, 62, this);
- }
- } catch (DataNotAvailable ex) {
- this.view.handleException(this.row(), this.col(), ex);
- }
-
- return true;
- }
-
- int findCursorPos(int x) throws DataNotAvailable {
- if (x < -this.offset) {
- return 0;
- } else {
- int total = this.offset + x;
- FontMetrics fm = this.view.getCellFontMetrics(this);
- Data data = this.readData();
- String text = data.toString();
- int len = text.length();
- Image im = data.toImage();
- int align = this.view.getCellAlignment(this);
- if (align == 0) {
- if (this.offset == 0) {
- total -= 7;
- if (im != null) {
- int imageOffset = im.getWidth(this) + 2;
- int w = this.view.getColumnWidth(this.getCoordinates().col);
- int sw = fm.stringWidth(text);
- if (imageOffset + sw + 2 + 5 <= w) {
- total -= imageOffset;
- }
- }
- }
- } else if (align == 2) {
- int sw = fm.stringWidth(text);
- int w = this.view.getColumnWidth(this.getCoordinates().col);
- if (sw <= w - 10) {
- total = total - w + sw + 5;
- }
- } else if (align == 1) {
- int sw = fm.stringWidth(text);
- int w = this.view.getColumnWidth(this.getCoordinates().col);
- if (sw <= w) {
- total -= (w - sw) / 2;
- }
- }
-
- if (total <= 0) {
- return 0;
- } else {
- for(int i = 1; i <= len; ++i) {
- if (fm.stringWidth(text.substring(0, i - 1) + fm.charWidth(text.charAt(i - 1)) / 2) > total) {
- return i;
- }
- }
-
- return len;
- }
- }
- }
-
- public CellHints getHints() {
- return this.view.getHintsForCell(this, this.coords.row, this.coords.col);
- }
-
- public Data getData() throws DataNotAvailable {
- return this.dataSource.getData(this.coords);
- }
-
- public Data readData() throws DataNotAvailable {
- return this.dataSource.readData(this.coords.row, this.coords.col);
- }
-
- void deleteChar(Data data) {
- if (this.cursorPos != 0) {
- data.deleteChar(this.cursorPos);
- --this.cursorPos;
- }
- }
-
- void deleteSelection(Data data) {
- int hlLeft = Math.min(this.hlFirst, this.hlLast);
- int hlRight = Math.max(this.hlFirst, this.hlLast);
-
- for(int i = hlLeft; i < hlRight; ++i) {
- data.deleteChar(hlLeft + 1);
- }
-
- this.cursorPos = hlLeft;
- }
-
- public boolean keyEvent(Event e) {
- Data data;
- try {
- data = this.getData();
- } catch (DataNotAvailable ex) {
- this.view.handleException(this.row(), this.col(), ex);
- return true;
- }
-
- char c = (char)e.key;
- boolean changed = false;
- boolean handled = false;
- boolean editable = data.isEditable(this.coords.row, this.coords.col) || this.getHints().editable();
- if (editable || e.id != 403 && e.id != 401) {
- if (!editable && e.id == 402) {
- return this.view.generateEvent(e, 59, this);
- } else if (e.id == 403) {
- if (e.shiftDown() && !this.selectionMade) {
- this.selectionMade = true;
- this.hlFirst = this.hlLast = this.cursorPos;
- } else if (!e.shiftDown()) {
- this.selectionMade = false;
- }
-
- switch (c) {
- case 'Ϩ':
- case 'Ϭ':
- if (this.cursorPos != 0) {
- this.cursorPos = 0;
- changed = true;
- }
- break;
- case 'ϩ':
- case 'ϭ':
- int len = data.toString().length();
- if (this.cursorPos != len) {
- this.cursorPos = len;
- changed = true;
- }
- case 'Ϫ':
- case 'ϫ':
- default:
- break;
- case 'Ϯ':
- if (this.cursorPos > 0) {
- --this.cursorPos;
- changed = true;
- }
- break;
- case 'ϯ':
- if (this.cursorPos < data.toString().length()) {
- ++this.cursorPos;
- changed = true;
- }
- }
-
- if (this.selectionMade) {
- this.hlLast = this.cursorPos;
- }
-
- if (changed) {
- this.view.redrawCell(this);
- }
-
- return this.view.generateEvent(e, 58, this);
- } else {
- if (e.id == 401) {
- if (!this.keyPressedYet) {
- if (e.key < 33 || e.key > 122) {
- return this.view.generateEvent(e, 58, this);
- }
-
- data.clearText();
- data.appendChar(c);
- this.keyPressedYet = true;
- this.view.setCapture();
- this.cursorPos = 1;
- if (this.defaultCell) {
- this.view.addCellFromDefault(this);
- }
- } else {
- if (this.selectionMade) {
- this.deleteSelection(data);
- this.selectionMade = false;
- if (c == '\b' || c == 127) {
- return this.view.generateEvent(e, 58, this);
- }
- }
-
- if (e.key == 27) {
- this.cursorPos = 0;
- this.keyPressedYet = false;
- handled = this.view.generateEvent(e, 54, this);
- } else if (c == '\b') {
- this.deleteChar(data);
- handled = this.view.generateEvent(e, 57, this);
- } else if (c == 127) {
- if (this.cursorPos != data.toString().length()) {
- ++this.cursorPos;
- this.deleteChar(data);
- handled = this.view.generateEvent(e, 57, this);
- }
- } else if (this.cursorPos == data.toString().length()) {
- data.appendChar(c);
- ++this.cursorPos;
- if (this.defaultCell) {
- this.view.addCellFromDefault(this);
- }
-
- handled = this.view.generateEvent(e, 57, this);
- } else {
- data.insertChar(this.cursorPos, c);
- ++this.cursorPos;
- if (this.defaultCell) {
- this.view.addCellFromDefault(this);
- }
- }
- }
-
- this.view.redrawCell(this);
- handled |= this.view.generateEvent(e, 58, this);
- } else if (e.id == 402) {
- this.view.generateEvent(e, 59, this);
- }
-
- return handled;
- }
- } else {
- return this.view.generateEvent(e, 58, this);
- }
- }
-
- public boolean loseFocusOnArrow() {
- return this.selected & !this.keyPressedYet;
- }
-
- public void activateCursor() {
- this.selected = true;
- this.view.redrawCell(this);
- }
-
- public void deactivateCursor() {
- this.selected = false;
- this.view.redrawCell(this);
- }
-
- public boolean canLoseFocus() {
- try {
- this.dataSource.commitData();
- return true;
- } catch (Exception ex) {
- this.view.handleException(this.row(), this.col(), ex);
- return false;
- }
- }
-
- public boolean focusEvent(Event e) {
- if (e.id == 1004) {
- this.selected = true;
- this.view.generateEvent(e, 55, this);
- this.view.redrawCell(this);
- } else {
- this.selected = false;
- this.keyPressedYet = false;
- this.selectionMade = false;
- this.offset = 0;
- this.cursorPos = 0;
- this.view.generateEvent(e, 56, this);
-
- try {
- this.dataSource.commitData();
- } catch (Exception ex) {
- this.view.handleException(this.row(), this.col(), ex);
- }
-
- this.view.redrawAroundCell(this);
- }
-
- return true;
- }
-
- public boolean actionEvent(Event e) {
- this.keyPressedYet = false;
- this.view.redraw(true);
- this.offset = 0;
- return true;
- }
-
- public void drawCell(Graphics g, CellHints hints) {
- Data data;
- try {
- data = this.readData();
- } catch (DataNotAvailable ex) {
- this.view.handleException(this.row(), this.col(), ex);
- data = new ImageStringData(this.dataSource, "");
- }
-
- Rectangle r = hints.bounds();
- FontMetrics fm = this.view.getCellFontMetrics(this);
- fm.getAscent();
- int sw = fm.stringWidth(data.toString());
- int imageOffset = 0;
- int origX = r.x;
- int origWidth = r.width;
- hints.setBackground(g);
- g.fillRect(r.x, r.y, r.width - 1, r.height - 1);
- Image im = data.toImage();
- switch (hints.alignment()) {
- case 0:
- if (im != null) {
- imageOffset = im.getWidth(this) + 2;
- }
-
- if (imageOffset + sw + 2 + 5 <= r.width && im != null) {
- r.x += 5;
- ++r.y;
- g.drawImage(im, r.x, r.y, this);
- --r.y;
- } else {
- imageOffset = 0;
- }
-
- r.x = origX + imageOffset + 5;
- break;
- case 1:
- if (sw > r.width - 10) {
- r.x += 5;
- } else {
- r.x += (r.width - sw) / 2;
- }
- break;
- case 2:
- if (im != null) {
- imageOffset = im.getWidth(this);
- }
-
- if (imageOffset + 2 + sw + 10 + 3 <= r.width && im != null) {
- r.x = r.x + r.width - sw - imageOffset - 2 - 5 - 3;
- ++r.y;
- g.drawImage(im, r.x, r.y, this);
- --r.y;
- r.x = origX + r.width - sw - 5 - 3;
- } else {
- imageOffset = 0;
- if (sw > r.width - 10) {
- r.x += 5;
- } else {
- r.x = origX + r.width - sw - 5 - 3;
- }
- }
- }
-
- hints.setForeground(g);
- r.width = r.width + origX - r.x - 5;
- this.startX = r.x;
- this.drawText(data, g, r, fm, hints);
- r.width = origWidth;
- if (this.selected && this.keyPressedYet) {
- int cpos = 0;
- if (data.toString().length() > 0) {
- cpos = fm.stringWidth(data.subString(this.toLeftOfCell, this.cursorPos));
- }
-
- g.drawLine(r.x + cpos, r.y + 2, r.x + cpos, r.y + fm.getHeight());
- }
-
- r.x = origX;
- hints.drawBoundary(g);
- }
-
- protected String chopString(Data data, Rectangle r, FontMetrics fm) {
- String text = data.toString();
- int w = this.view.getColumnWidth(this.coords.col);
- this.chopIndex = 1;
- if (text.length() == 0) {
- return text;
- } else {
- this.setOffset(text, r, fm);
- if (this.offset == 0) {
- this.toLeftOfCell = 0;
- } else {
- while(this.toLeftOfCell < text.length()) {
- int left = fm.stringWidth(text.substring(0, this.toLeftOfCell));
- if (left >= this.offset + 5) {
- --this.toLeftOfCell;
- break;
- }
-
- ++this.toLeftOfCell;
- }
-
- this.toLeftOfCell -= this.toLeftOfCell < text.length() ? 0 : 1;
- text = text.substring(this.toLeftOfCell, text.length());
- }
-
- String t;
- do {
- t = text.substring(0, this.chopIndex);
- } while(fm.stringWidth(t) < w - 10 && this.chopIndex++ < text.length());
-
- return text.substring(0, --this.chopIndex);
- }
- }
-
- protected void drawText(Data data, Graphics g, Rectangle r, FontMetrics fm, CellHints hints) {
- String text = this.chopString(data, r, fm);
- if (this.selected && !this.keyPressedYet) {
- hints.setForeground(g);
- g.fillRect(r.x - 1, r.y + 3, fm.stringWidth(text.toString()) + 4, fm.getHeight() - 1);
- hints.setBackground(g);
- g.drawString(text, r.x, r.y + fm.getAscent() + 2);
- } else if (this.selected && !this.selectionMade) {
- g.drawString(text, r.x, r.y + fm.getAscent() + 2);
- } else if (this.selected && this.selectionMade) {
- int hlLeft = Math.min(this.hlFirst, this.hlLast);
- int hlRight = Math.max(this.hlFirst, this.hlLast);
- hlRight = Math.min(hlRight, this.chopIndex);
- if (this.toLeftOfCell != 0) {
- hlLeft = Math.max(this.hlFirst, this.toLeftOfCell);
- }
-
- String t = text.substring(0, hlLeft);
- g.drawString(t, r.x, r.y + fm.getAscent() + 2);
- int pixelHLStart = fm.stringWidth(t) + r.x - this.offset;
- t = text.substring(hlLeft, hlRight);
- int pixelHLStop = fm.stringWidth(t) + pixelHLStart;
- hints.setForeground(g);
- g.fillRect(pixelHLStart, r.y + 2, pixelHLStop - pixelHLStart, fm.getHeight() - 1);
- hints.setBackground(g);
- g.drawString(t, pixelHLStart, r.y + fm.getAscent() + 2);
- hints.setForeground(g);
- t = text.substring(hlRight, text.length());
- g.drawString(t, pixelHLStop, r.y + fm.getAscent() + 2);
- } else {
- g.drawString(text, r.x, r.y + fm.getAscent() + 2);
- }
- }
-
- void setOffset(String text, Rectangle r, FontMetrics fm) {
- String sub = text.substring(0, this.cursorPos);
- int toCursor = fm.stringWidth(sub);
- int fudge = 3;
- if (!this.selected) {
- this.offset = 0;
- } else if (toCursor > r.width - fudge) {
- this.offset = -r.width + toCursor + fudge;
- } else {
- this.offset = 0;
- }
- }
-
- int textLeft(int txtWidth, Rectangle r) {
- switch (this.view.getCellAlignment(this)) {
- case 0:
- default:
- return r.x + 5;
- case 1:
- if (txtWidth > r.width) {
- return r.x;
- }
-
- return r.x + (r.width - txtWidth) / 2;
- case 2:
- return txtWidth > r.width ? r.x + 5 : r.x + r.width - txtWidth - 5;
- }
- }
-
- public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h) {
- if ((flags & 192) != 0) {
- return false;
- } else if ((flags & 32) != 0) {
- this.view.redraw(true);
- return false;
- } else {
- return true;
- }
- }
-
- public String toString() {
- try {
- return this.readData().toString();
- } catch (DataNotAvailable var1) {
- return "ERROR getting data";
- }
- }
-
- public String stats() {
- try {
- return "BasicCell: row=" + this.coords.row + " col=" + this.coords.col + " text=" + this.readData().toString();
- } catch (DataNotAvailable var1) {
- return "BasicCell: row=" + this.coords.row + " col=" + this.coords.col + " could not retrieve data";
- }
- }
- }
-